<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/

abstract class Cache {

	public static function clear()
	{
		$cache = dir(CACHE_PATH);
		while($file = $cache->read()) {
			if (!is_dir(CACHE_PATH.$file) && $file != '.htaccess' && $file != '.cvsignore') {
				unlink(CACHE_PATH.$file);
			}
		}
		$cache->close();
	}

	public static function clear_conf()
	{
		if (is_file(CACHE_PATH.'Dragonfly_Config')) unlink(CACHE_PATH.'Dragonfly_Config');
		$cache = dir(CACHE_PATH);
		while($file = $cache->read()) {
			if (!is_dir(CACHE_PATH.$file) && false !== strpos($file, 'config')) {
				unlink(CACHE_PATH.$file);
			}
		}
		$cache->close();
	}

	private static function array_parse($array, $space='  ')
	{
		$return = '';
		foreach($array as $key => $value) {
			$key = is_int($key) ? $key : "'$key'";
			$return .= "$space$key => ";
			if (is_array($value)) {
				$return .= "array(\n".Cache::array_parse($value, "$space  ")."$space),\n";
			} else {
				if (!is_int($value)) {
					$value = str_replace('\\', "\\\\", trim($value));
					$value = "'".str_replace("'", "\\'", $value)."'";
				}
				$return .= $value.",\n";
			}
		}
		return $return;
	}

	public static function array_save($name, $module_name='config', $array=false)
	{
		if (is_dir(CACHE_PATH) && is_writable(CACHE_PATH)) {
			$filename = CACHE_PATH.$module_name."_$name.php";
			$data = "<?php\nif (!defined('CPG_NUKE')) { exit; }\n";
			if (is_array($array)) {
				$data .= "\$$name = array(\n".Cache::array_parse($array).");";
			} else {
				global $$name;
				if (is_array($$name)) { $data .= "\$$name = array(\n".Cache::array_parse($$name).");"; }
			}
			file_write($filename, $data);
		}
	}

	public static function array_load($name, $module_name='config', $global=true)
	{
		if ($global) global $$name;
		$filename = CACHE_PATH."{$module_name}_{$name}.php";
		if (file_exists($filename)) {
			include($filename);
			if (!defined('PHP_AS_NOBODY')) { define_nobody($filename); }
			return $$name;
		}
		return false;
	}

	public static function array_delete($name, $module_name='config') {
		Cache::remove($name, $module_name);
	}

	public static function remove($name, $module_name='config')
	{
		$filename = CACHE_PATH.($name == 'MAIN_CFG' ? 'Dragonfly_Config' : "{$module_name}_{$name}.php");
		if (is_dir(CACHE_PATH) && is_writable(CACHE_PATH) && file_exists($filename)) {
			unlink($filename);
		}
		clearstatcache();
	}

	public static function defines_save($name, $module_name, $defines)
	{
		$cache_dir = BASEDIR.'cache';
		if (is_dir(CACHE_PATH) && is_writable(CACHE_PATH)) {
			$filename = CACHE_PATH."{$module_name}_{$name}.php";
			$data = "<?php\nif (!defined('CPG_NUKE')) { exit; }\n";
			foreach ($defines as $name => $value) {
				if (!is_int($value)) {
					$value = str_replace('\\', '\\\\', trim($value));
					$value = "'".str_replace("'", "\\'", $value)."'";
				}
				$data .= "define('$name', $value);\n";
			}
			file_write($filename, $data);
		}
	}

	public static function defines_load($name, $module_name)
	{
		$filename = CACHE_PATH."{$module_name}_{$name}.php";
		if (file_exists($filename)) {
			include_once($filename);
			return true;
		}
		return false;
	}

}
